﻿using System;

namespace winS.Unity
{
    /// <summary>
    /// 外部句柄
    /// </summary>
    public abstract class Handle
    {
        /// <summary>
        /// 强制停止事件
        /// </summary>
        public event Action onStop;
        /// <summary>
        /// 结束事件.
        /// 即使被Stop, 也会触发该事件
        /// </summary>
        public event Action onFinished;

        private bool running = true;

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            if (running)
            {
                running = false;
                onStop?.Invoke();
                Finish();
                return;
            }
        }

        /// <summary>
        /// 结束
        /// </summary>
        protected void Finish()
        {
            onFinished?.Invoke();
            onFinished = null;
            onStop = null;
            running = false;
        }
    }
}